package com.ysu.zyw.service;

import com.ysu.zyw.dao.BlogArticleMapper;
import com.ysu.zyw.dao.BlogCategoryMapper;
import com.ysu.zyw.dao.BlogMessageMapper;
import com.ysu.zyw.dao.BlogUserMapper;
import com.ysu.zyw.modal.vo.*;
import com.ysu.zyw.other.exception.KnownException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by zhang_yao_wu1993 on 15/4/25.
 *
 * @author by Zerur
 */
@Service("blogPageService")
public class BlogPageService {
    private BlogUserMapper blogUserMapper;
    private BlogMessageMapper blogMessageMapper;
    private BlogCategoryMapper blogCategoryMapper;
    private BlogArticleMapper blogArticleMapper;

    public BlogUserMapper getBlogUserMapper() {
        return blogUserMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogUserMapper(BlogUserMapper blogUserMapper) {
        this.blogUserMapper = blogUserMapper;
    }

    public BlogMessageMapper getBlogMessageMapper() {
        return blogMessageMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogMessageMapper(BlogMessageMapper blogMessageMapper) {
        this.blogMessageMapper = blogMessageMapper;
    }

    public BlogCategoryMapper getBlogCategoryMapper() {
        return blogCategoryMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogCategoryMapper(BlogCategoryMapper blogCategoryMapper) {
        this.blogCategoryMapper = blogCategoryMapper;
    }

    public BlogArticleMapper getBlogArticleMapper() {
        return blogArticleMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogArticleMapper(BlogArticleMapper blogArticleMapper) {
        this.blogArticleMapper = blogArticleMapper;
    }




    @Transactional
    public void setBlogMainPageModelAndView(ModelAndView modelAndView, String category, String page) {
        if(Integer.parseInt(page) < 0) {
            throw new KnownException("请求的页面不存在");
        }

        // get loginStatusMessage
        String nickname = this.getCurrentNickname();

        // get blogMenuList
        List<BlogMenu> blogMenuList = blogCategoryMapper.selectBlogMenuList();
        Integer articleCount = 0;
        if(Integer.parseInt(category) == 0) {
            articleCount = blogArticleMapper.selectBlogArticleCount();
        } else {
            articleCount = blogArticleMapper.selectArticleCountByCategoryId(Integer.parseInt(category));
        }
        BlogMenu blogMenu = new BlogMenu(0, "全部", "", "", articleCount);    // 添加全部选项
        blogMenuList.add(0, blogMenu);

        // get currentMenu
        String currentMenu = "";

        for(Iterator<BlogMenu> iterator = blogMenuList.iterator(); iterator.hasNext(); ) {
            BlogMenu i = iterator.next();
            if(i.getId() == Integer.parseInt(category)) {                 // 添加 active
                i.setActive("active");
                i.setTeal("teal");
                currentMenu = i.getMenu();
            }
        }

        // get blogMainPageArticleList
        Integer startIndex = Integer.parseInt(page) * 10 - 10;
        PageAndCategory pageAndCategory = new PageAndCategory(startIndex, Integer.parseInt(category));
        List<BlogMainPageArticle> blogMainPageArticleList = null;
        if(Integer.parseInt(category) == 0) {
            blogMainPageArticleList = blogArticleMapper.selectAllBlogMainPageArticleByPage(pageAndCategory);
        } else {
            blogMainPageArticleList = blogArticleMapper.selectBlogMainPageArticleByPageAndCategory(pageAndCategory);
        }

        // get blogMainPagePaginationList
        int articlePageCount = (int) Math.ceil(articleCount / 10.0);
        articlePageCount = articlePageCount == 0 ? 1 : articlePageCount; // 0 条数据
        if(Integer.parseInt(page) > articlePageCount) {
            throw new KnownException("请求的页面不存在");
        }
        List<BlogMainPagePagination> blogMainPagePaginationList = new ArrayList<BlogMainPagePagination>();
        for(int i = 0; i < articlePageCount; i++) {
            String active = "";
            if(i + 1 == Integer.parseInt(page)) {
                active = "active";
            }
            blogMainPagePaginationList.add(new BlogMainPagePagination(active, i + 1, "/MainPage/" + Integer.parseInt(category) + "/" + (i + 1)));
        }


        // fill vo
        BlogMainPageVo blogMainPageVo = new BlogMainPageVo(nickname, blogMenuList, currentMenu, blogMainPageArticleList, blogMainPagePaginationList);
        modelAndView.addObject("blogMainPageVo", blogMainPageVo);
    }

    @Transactional
    public void setBlogMessagePageModelAndView(ModelAndView modelAndView, String page) {
        // get loginStatusMessage
        String nickname = this.getCurrentNickname();

        // get blogMessageList
        Integer startIndex = Integer.parseInt(page) * 10 - 10;
        List<BlogMessage> blogMessageList = blogMessageMapper.selectBlogMessageList(startIndex);

        // get messagePaginationList
        List<BlogMessagePagination> blogMessagePaginationList = new ArrayList<BlogMessagePagination>();
        int messagePageCount = (int) Math.ceil(blogMessageMapper.selectBlogMessagePageCount() / 10.0);
        messagePageCount = messagePageCount == 0 ? 1 : messagePageCount; // 0 条数据
        if(Integer.parseInt(page) > messagePageCount) {
            throw new KnownException("请求的页面不存在");
        }
        for(int i = 0; i < messagePageCount; i++) {
            String active = "";
            if(i + 1 == Integer.parseInt(page)) {
                active = "active";
            }
            blogMessagePaginationList.add(new BlogMessagePagination(active, i + 1, "/MessagePage/" + (i + 1)));
        }

        // fill vo
        BlogMessagePageVo blogMessagePageVo = new BlogMessagePageVo(nickname, blogMessageList, blogMessagePaginationList);
        modelAndView.addObject("blogMessagePageVo", blogMessagePageVo);
    }

    @Transactional
    public void setBlogUploadPageModelAndView(ModelAndView modelAndView) {
        List<BlogCategory> categoryList = blogCategoryMapper.selectBlogCategoryList();

        BlogUploadPageVo blogUploadPageVo = new BlogUploadPageVo(categoryList);

        modelAndView.addObject("blogUploadPageVo", blogUploadPageVo);
    }

    @Transactional
    public void setBlogArticlePageModelAndView(ModelAndView modelAndView, String uuid) {
        blogArticleMapper.updateArticleClickRateAddOne(uuid);
        BlogArticlePageVo blogArticlePageVo = blogArticleMapper.selectBlogArticlePageVo(uuid);
        modelAndView.addObject("blogArticlePageVo", blogArticlePageVo);
    }

    @Transactional
    public void setBlogIndividualCenterModelAndView(ModelAndView modelAndView) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        BlogIndividualCenterVo blogIndividualCenterVo = blogUserMapper.selectBlogIndividualCenterVoByUsername(username);
        if(blogIndividualCenterVo.getAuthority().equals("ROLE_ADMIN")) {
            blogIndividualCenterVo.setAuthority("管理员");
        } else {
            blogIndividualCenterVo.setAuthority("普通用户");
        }

        modelAndView.addObject("blogIndividualCenterVo", blogIndividualCenterVo);
    }

    @Transactional
    public void setBlogManagePageModelAndView(ModelAndView modelAndView) {
        List<BlogManagePageCategory> blogManagePageCategoryList = blogCategoryMapper.selectBlogManagePageCategory();
        List<BlogManagePageArticle> blogManagePageArticleList = blogArticleMapper.selectBlogManagePageArticle();

        BlogManagePageVo blogManagePageVo = new BlogManagePageVo(blogManagePageCategoryList, blogManagePageArticleList);
        modelAndView.addObject("blogManagePageVo", blogManagePageVo);
    }

    private String getCurrentNickname() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String nickname = null;
        if(username.equals("anonymousUser")) {
            nickname = "你尚未登陆";
        } else {
            nickname = blogUserMapper.selectNicknameByUsername(username);
        }
        return nickname;
    }
}
